package com.innovation.chengzhangriji.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.innovation.chengzhangriji.Config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileUtils {

    /**
     * 获取文件后缀名
     *
     * @param filePath 文件地址
     * @return 文件后缀名
     */
    public static String getFileExtension(String filePath) {
        String filenameArray[] = filePath.split("\\.");
        return filenameArray[filenameArray.length - 1];
    }

    /**
     * 获取文件后缀名
     *
     * @param file 文件
     * @return 文件后缀名
     */
    public static String getFileExtension(File file) {
        return getFileExtension(file.getPath());
    }

    /**
     * 拷贝文件
     */
    public static void copyFileUsingFileChannels(File source, File dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            if (inputChannel != null) {
                inputChannel.close();
            }
            if (outputChannel != null) {
                outputChannel.close();
            }
        }
    }

    /**
     * 文件转换Base64格式
     *
     * @param file 文件
     * @return BASE64
     */
    public static String encodeFileToBase64Binary(File file) {
        byte[] bytes = new byte[0];
        try {
            bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeToString(bytes, 0);
    }

    /**
     * 删除文件
     */
    public static void deleteFile(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                //noinspection ResultOfMethodCallIgnored
                file.delete();
            }
        }
    }

    /**
     * 保存位图
     */
    public static String saveBitmap(Context context, Bitmap bm) {
        File f = Config.createFile(context, Config.SCREENSHOT_DIR);
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 75, out);
            out.flush();
            out.close();
            return f.getPath();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bm != null && !bm.isRecycled()) {
                bm.recycle();
            }
            bm = null;
        }
        return null;
    }

    // 解析图片
    public static Size decodeImageSize(byte[] buffer) {
        int index = -1;
        // JPEG
        if((buffer[0x0000] & 0xFF) == 0xFF && (buffer[0x0001] & 0xFF) == 0xD8) {
            // 找到 FF C0 的位置，加 5 个字节 就是 高宽像素
            for(int i = 0; i < buffer.length; i++) {
                if((buffer[i] & 0xFF) == 0xFF && (buffer[i + 1] & 0xFF) == 0xC0) {
                    index = i + 5;
                    break;
                }
            }
            if(index == -1 && index + 3 > buffer.length) {
                return new Size(0, 0);
            }
            return new Size(((buffer[index + 2] & 0xFF) << 8) | (buffer[index + 3] & 0xFF), ((buffer[index] & 0xFF) << 8) | (buffer[index + 1] & 0xFF));
        }
        // PNG
        else if((buffer[0x0000] & 0xFF) == 0x89 && (buffer[0x0001] & 0xFF) == 0x50 && (buffer[0x0002] & 0xFF) == 0x4E && (buffer[0x0003] & 0xFF) == 0x47) {
            // 找到 IHDR 的位置，加 2 个字节 就是 宽高像素
            for(int i = 0; i < buffer.length; i++) {
                if((buffer[i] & 0xFF) == 0x49 && (buffer[i + 1] & 0xFF) == 0x48 && (buffer[i + 2] & 0xFF) == 0x44 && (buffer[i + 3] & 0xFF) == 0x52) {
                    index = i + 2;
                    break;
                }
            }
            if(index == -1 && index + 3 > buffer.length) {
                return new Size(0, 0);
            }
            return new Size(((buffer[index + 2] & 0xFF) << 8) | (buffer[index + 3] & 0xFF), ((buffer[index] & 0xFF) << 8) | (buffer[index + 1] & 0xFF));
        }
        return new Size(0, 0);
    }

    // 解析图片大小
    public static Size decodeImageSize(String file) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, options);
        Size size = new Size(options.outWidth, options.outHeight);
        return size;
    }
}
